package com.vic.dead_letter_queue.ttl;

import com.rabbitmq.client.*;
import com.vic.dead_letter_queue.Consts;
import com.vic.utils.RabbitmqUtils;

/**
 * 消费者
 */
public class Consumer02 {

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitmqUtils.getChannel();

        // 声明死信交换机(类型为 direct)
        channel.exchangeDeclare(Consts.DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);

        // 声明死信队列
        channel.queueDeclare(Consts.DEAD_QUEUE, true, false, false, null);

        // 死信交换机绑定死信队列
        channel.queueBind(Consts.DEAD_QUEUE, Consts.DEAD_EXCHANGE, Consts.DEAD_ROUTING_KEY);

        // 消息成功之后的回调
        DeliverCallback deliverCallback = (String consumerTag, Delivery message) -> {
            String msg = new String(message.getBody());
            System.out.println(msg);
        };
        // 取消消费者的回调
        CancelCallback cancelCallback = consumerTag -> {
            System.out.println("取消消费者时的回调接口");
        };
        // 消费者消费消息
        channel.basicConsume(Consts.DEAD_QUEUE, true, deliverCallback, cancelCallback);

        System.out.println("Consumer02 开始消费消息");
    }

}
